use anyhow::Result;
use cdb_core::{DatabaseManager, QueryProcessor, SchemaManager};
use cdb_ai::AiEngine;
use cdb_gui::CdbGui;

#[tokio::test]
async fn test_database_manager_creation() -> Result<()> {
    let manager = DatabaseManager::new().await?;
    assert_eq!(manager.list_connections().len(), 0);
    Ok(())
}

#[test]
fn test_database_manager_sync_creation() {
    let manager = DatabaseManager::new_sync();
    assert_eq!(manager.list_connections().len(), 0);
}

#[tokio::test]
async fn test_query_processor_creation() {
    let processor = QueryProcessor::new();
    assert_eq!(processor.get_history().len(), 0);
}

#[tokio::test]
async fn test_schema_manager_creation() {
    let manager = SchemaManager::new();
    let stats = manager.get_cache_stats();
    assert_eq!(stats.cached_databases, 0);
}

#[tokio::test]
async fn test_query_validation() {
    let processor = QueryProcessor::new();
    
    // Test empty query
    assert!(!processor.validate_sql("").unwrap());
    assert!(!processor.validate_sql("   ").unwrap());
    
    // Test valid query
    assert!(processor.validate_sql("SELECT * FROM users").unwrap());
}

#[tokio::test]
async fn test_connection_string_builder() {
    use cdb_core::database::{ConnectionString, DatabaseType};
    
    let conn_str = ConnectionString::new(DatabaseType::PostgreSQL)
        .host("localhost")
        .port(5432)
        .database("testdb")
        .username("user")
        .password("pass")
        .build();
    
    assert!(conn_str.contains("postgresql://"));
    assert!(conn_str.contains("localhost:5432"));
    assert!(conn_str.contains("testdb"));
}

#[tokio::test]
async fn test_database_type_properties() {
    use cdb_core::database::DatabaseType;
    
    assert_eq!(DatabaseType::PostgreSQL.default_port(), 5432);
    assert_eq!(DatabaseType::MySQL.default_port(), 3306);
    assert_eq!(DatabaseType::MongoDB.default_port(), 27017);
    
    assert!(DatabaseType::PostgreSQL.supports_sql());
    assert!(DatabaseType::MySQL.supports_sql());
    assert!(!DatabaseType::MongoDB.supports_sql());
    
    assert_eq!(DatabaseType::PostgreSQL.display_name(), "PostgreSQL");
    assert_eq!(DatabaseType::MySQL.display_name(), "MySQL");
}

#[tokio::test]
async fn test_ai_engine_creation() {
    let ai_engine = AiEngine::new_sync();
    // Test that AI engine can be created without errors
    assert!(ai_engine.generate_sql("SELECT * FROM users", None).await.is_ok());
}

#[test]
fn test_gui_creation() {
    let gui = CdbGui::new();
    // Test that GUI can be created
    assert_eq!(gui.get_history().len(), 0);
}

#[tokio::test]
async fn test_sql_generation() {
    let ai_engine = AiEngine::new_sync();
    let result = ai_engine.generate_sql("find all users", None).await;
    assert!(result.is_ok());
    
    let generated = result.unwrap();
    assert!(!generated.sql.is_empty());
    assert!(!generated.explanation.is_empty());
}

#[tokio::test]
async fn test_query_analysis() {
    let ai_engine = AiEngine::new_sync();
    let result = ai_engine.analyze_query("SELECT * FROM users").await;
    assert!(result.is_ok());
    
    let analysis = result.unwrap();
    assert!(!analysis.summary.is_empty());
}
